﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemotingObject
{
    public delegate void MyDelegate(string msg);

    /// <summary>
    /// 代理类
    /// </summary>
    public class RemotingObj : MarshalByRefObject
    {
        #region Event

        public event MyDelegate SubscribeAtServer; // 在客户端触发,在服务器订阅的事件
        public event MyDelegate SubscribeAtClient; // 在服务器触发,在客户端订阅的事件

        #endregion

        #region 方法

        /// <summary>
        /// 服务器触发事件
        /// </summary>
        /// <param name="msg"></param>
        public void TriggerAtClient(string msg)
        {
            if (SubscribeAtServer != null)
                SubscribeAtServer(msg);
        }

        /// <summary>
        /// 客户端触发事件
        /// </summary>
        /// <param name="msg"></param>
        public void TriggerAtServer(string msg)
        {
            if (SubscribeAtClient != null)
                SubscribeAtClient(msg);
        }

        /// <summary>
        /// 获取委托列表
        /// </summary>
        /// <returns></returns>
        public Delegate[] GetServerEventList()
        {
            return this.SubscribeAtClient.GetInvocationList();
        }

        #endregion

        #region Override

        /// <summary>
        /// 无限生命周期
        /// </summary>
        /// <returns></returns>
        public override object InitializeLifetimeService()
        {
            return null;
        }

        #endregion
    }
}
